<?php

namespace app\index\model;

use think\Model;

class Member extends Model
{
    protected $pk = 'memb_id';
    /**
     * 改变积分，如果从配置表下读取值，则不需要填写改变的值
     * @param string $uid  用户id
     * @param string $operate  操作方式
     * @param int $val  改变的值
     */
    public static function updatePoint($uid, $operate, $val){
        try {
            Member::where('memb_id', $uid)->inc('memb_point', $val)->update();
            insertPointLog($operate, $val);
            return mesg(0, 'success');
        } catch (\Throwable $th) {
            throw $th;
        }
    }
    /**
     * 改变余额
     * @param int $userid  要改变的用户id
     * @param int $val  默认使用配置的值
     * @param string $operate  操作方式
     */
    public static function updateBalance($userid, $val, $operate){
        try {
            Member::where('memb_id', $userid)->inc('memb_balance', $val)->update();
            OperateLog::create([
                'member_id' => $userid,
                'operate'   => $operate,
                'value'     => $val
            ]); 
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取抽奖次数
     * @return int
     */
    public function getCount(){
        return $this->where('memb_id', session('user.id'))->value('memb_count');
    }

    /**
     * 抽奖
     * @param array $pro  奖品数组
     */
    public function getLottery($pro){
        foreach($pro as $k => $v){
            $arr[$k] = $v['v'];
        }
        $id = getRand($arr);
        
        $this->memb_count--;
        $this->save();
        return $id;
    }

    /**
     * 更新抽奖次数
     */
     public static function updateCount($uid, $v){
         try {
            Member::where('memb_id', $uid)->inc('memb_count', $v)->update();
        } catch (\Throwable $th) {
            throw $th;
        }
     }

    public function updateInfo($param, $id){
        try {
            $this->where('memb_id', $id)->update($param);
            return mesg(0,'ok');
        } catch (\Throwable $th) {
            return mesg(-1, 'fali');
        }
    }

}
